const crypto = require('crypto');
let key = ''
exports.encrypt = encrypt;
exports.decrypt = decrypt;


function encrypt(data) {
    if(typeof data == 'object') data = JSON.stringify(data);

    let cipher = crypto.createCipher('aes-128-cbc', key);
    let crypted = cipher.update(data, 'utf8', 'binary');
    crypted += cipher.final('binary');
    crypted = Buffer.from(crypted, 'binary').toString('base64');
    return crypted;
}


function decrypt(crypted) {
    crypted = Buffer.from(crypted, 'base64').toString('binary');
    let decipher = crypto.createDecipher('aes-128-cbc', key);
    let decoded = decipher.update(crypted, 'binary', 'utf8');
    decoded += decipher.final('utf8');

    try{ decoded = JSON.parse(decoded) } catch(e) {}

    return decoded;
}
